<?php

App::uses('AppController', 'Controller');

class TimesheetsController extends AppController {
	
	public $helpers = array('Html', 'Form');
	public $uses = array('Client','Timesheet','Task','User','Holiday');
	
	public function index(){
		//$this->request->data['Timesheet']['user_id'] = $this->Auth->user('id'); //Added this line
		$this->set('timesheets', $this->Timesheet->find('all', array('conditions' => array('Timesheet.user_id =' => $this->Auth->user('id')))));
	}
	
	public function indexall(){
		//$this->request->data['Timesheet']['user_id'] = $this->Auth->user('id'); //Added this line
		$this->set('timesheets', $this->Timesheet->find('all', array('conditions' => array('Timesheet.department_id =' => $this->Auth->user('department_id')))));
		$this->set('users', $this->User->find('all', array('conditions' => array('User.department_id =' => $this->Auth->user('department_id')))));
	}
	
	public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid timesheet'));
        }

        $timesheet = $this->Timesheet->findById($id);
        if (!$timesheet) {
            throw new NotFoundException(__('Invalid timesheet'));
        }
		
		$this->set('clients', $this->Client->find('all'));//get the department from the departments table
		$this->set('tasks', $this->Task->find('all'));//get the department from the departments table
		$this->set('users', $this->User->find('all', array('conditions' => array('User.department_id =' => $this->Auth->user('department_id')))));
        $this->set('timesheet', $timesheet);
    }
	
	public function add($weekending) {
		if ($this->request->is('post')) {
			$this->request->data['Timesheet']['user_id'] = $this->Auth->user('id'); //Added this line
			$this->request->data['Timesheet']['department_id'] = $this->Auth->user('department_id');
			$this->request->data['Timesheet']['title'] = date('Y-m-d',$weekending); //Added this line
			$this->request->data['Timesheet']['content'] = json_encode($this->data['content']); //Added this line
			
			if ($this->Timesheet->save($this->request->data)) {
				$this->Session->setFlash('Your timesheet has been saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
		$this->set('weekending', $weekending);	
		$this->set('clients', $this->Client->find('all'));
		$this->set('tasks', $this->Task->find('all', array('conditions' => array('Task.department_id =' => $this->Auth->user('department_id'))) ));
		$this->set('holidays', $this->Holiday->find('all'));
	}
	
	public function edit($id = null) {
		if (!$id) {
			throw new NotFoundException(__('Invalid timesheet'));
		}
	
		$timesheet = $this->Timesheet->findById($id);
		if (!$timesheet) {
			throw new NotFoundException(__('Invalid timesheet'));
		}
	
		if ($this->request->is('timesheet') || $this->request->is('put')) {
			$this->Timesheet->id = $id;
			$this->request->data['Timesheet']['content'] = json_encode($this->data['content']); //Added this line
			if ($this->Timesheet->save($this->request->data)) {
				$this->Session->setFlash('Your timesheet has been updated.');
				$this->redirect(array('action' => 'edit', $id));
			} else {
				$this->Session->setFlash('Unable to update your timesheet.');
			}
		}
	
		if (!$this->request->data) {
			$this->request->data = $timesheet;
		}
			
		$this->set('clients', $this->Client->find('all'));//get the department from the departments table
		$this->set('tasks', $this->Task->find('all', array('conditions' => array('Task.department_id =' => $this->Auth->user('department_id'))) ));
		$this->set('timesheet', $timesheet);
		$this->set('holidays', $this->Holiday->find('all'));
	}
	
	public function delete($id) {
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
	
		if ($this->Client->delete($id)) {
			$this->Session->setFlash('The client with id: ' . $id . ' has been deleted.');
			$this->redirect(array('action' => 'index'));
		}
	}
}
